package by.dezhits.makeanote.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import by.dezhits.makeanote.NoteActivity;
import by.dezhits.makeanote.R;
import by.dezhits.makeanote.adapter.NotesCursorAdapter;
import by.dezhits.makeanote.content.contract.NotesContract;

public class NotesFragment extends ListFragment {

	private NotesCursorAdapter notesCursorAdapter;

	private static final String NO_NOTES = "Nothing here";
	private static final int NOTES_LOADER = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);
		notesCursorAdapter = new NotesCursorAdapter(this.getActivity(), null);
		this.setListAdapter(notesCursorAdapter);
		resetList(null);
		return view;
	}

	@Override
	public void onStart() {
		super.onStart();
		this.setEmptyText(NO_NOTES);

	}

	@Override
	public void onListItemClick(ListView list, View v, int position, long id) {
		Intent intent = new Intent(this.getActivity(), NoteActivity.class);
		Cursor cursor = (Cursor) list.getItemAtPosition(position);
		intent.putExtra(getString(R.string.id), cursor.getInt(cursor
				.getColumnIndex(NotesContract.NotesColumns.ID)));
		intent.putExtra(getString(R.string.title), cursor.getString(cursor
				.getColumnIndex(NotesContract.NotesColumns.TITLE)));
		intent.putExtra(getString(R.string.text), cursor.getString(cursor
				.getColumnIndex(NotesContract.NotesColumns.TEXT)));
		intent.putExtra(getString(R.string.folder), cursor.getString(cursor
				.getColumnIndex(NotesContract.NotesColumns.FOLDER)));
		intent.putExtra(getString(R.string.closed), cursor.getString(cursor
				.getColumnIndex(NotesContract.NotesColumns.CLOSED)));
		intent.putExtra(getString(R.string.request_code), NoteActivity.REWITE_NOTE);
		startActivity(intent);
	}

	public void resetList(final String selection) {
		getLoaderManager().restartLoader(NOTES_LOADER, null,
				new LoaderCallbacks<Cursor>() {

					@Override
					public Loader<Cursor> onCreateLoader(int paramInt,
							Bundle paramBundle) {
						return new CursorLoader(NotesFragment.this
								.getActivity(),
								NotesContract.NotesColumns.CONTENT_URI, null,
								selection, null, NotesContract.NotesColumns.ID);
					}

					@Override
					public void onLoadFinished(Loader<Cursor> paramLoader,
							Cursor paramD) {
						notesCursorAdapter.swapCursor(paramD);
						Log.d("Shit", String.valueOf(paramD.getCount()));
					}

					@Override
					public void onLoaderReset(Loader<Cursor> paramLoader) {
						notesCursorAdapter.swapCursor(null);
					}
				});

	}

}
